package io.renren.modules.eightapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.eightapp.dao.EiPayInfoDao;
import io.renren.modules.eightapp.dto.EiCostInfoDTO;
import io.renren.modules.eightapp.dto.EiPayInfoDTO;
import io.renren.modules.eightapp.entity.EiCostInfoEntity;
import io.renren.modules.eightapp.entity.EiPayInfoEntity;
import io.renren.modules.eightapp.service.EiPayInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Mark 1132115960@gmail.com
 * @since 1.0.0 2022-12-27
 */
@Service
public class EiPayInfoServiceImpl extends CrudServiceImpl<EiPayInfoDao, EiPayInfoEntity, EiPayInfoDTO> implements EiPayInfoService {

    @Autowired
    private EiPayInfoDao payInfoDao;

    @Override
    public QueryWrapper<EiPayInfoEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<EiPayInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public List<EiPayInfoDTO> getPayInfoAllDataList() {
        LambdaQueryWrapper<EiPayInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select();
        List<EiPayInfoEntity> entityList = payInfoDao.selectList(queryWrapper);
        List<EiPayInfoDTO> dtoList = ConvertUtils.sourceToTarget(entityList, EiPayInfoDTO.class);
        return dtoList;
    }

    @Override
    public List<EiPayInfoDTO> getPayInfoDataListByOwnerId(Integer ownerId) {
        LambdaQueryWrapper<EiPayInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(EiPayInfoEntity::getOwnerId, ownerId);
        List<EiPayInfoEntity> entityList = payInfoDao.selectList(queryWrapper);
        List<EiPayInfoDTO> dtoList = ConvertUtils.sourceToTarget(entityList, EiPayInfoDTO.class);
        return dtoList;
    }

    @Override
    public List<EiPayInfoDTO> getPayInfoDataListByMemo(String memo) {
        LambdaQueryWrapper<EiPayInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(EiPayInfoEntity::getMemo, memo);
        List<EiPayInfoEntity> entityList = payInfoDao.selectList(queryWrapper);
        List<EiPayInfoDTO> dtoList = ConvertUtils.sourceToTarget(entityList, EiPayInfoDTO.class);
        return dtoList;
    }

    @Override
    public Integer getPayInfoTotalByYear(String year) {
        int total = 0;
        String yearStart = year.concat("-01-01 00:00:00");
        String yearEnd = year.concat("-12-31 23:59:59");
        LambdaQueryWrapper<EiPayInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(EiPayInfoEntity::getTotal).between(EiPayInfoEntity::getPayTime, yearStart, yearEnd);
        List<EiPayInfoEntity> entityList = payInfoDao.selectList(queryWrapper);
        for (int i = 0; i < entityList.size(); i++) {
            total += entityList.get(i).getTotal();
        }
        return total;
    }
}